// JavaScript Document
$(document).ready(function (e)
{
    var userkind = $('#ctl00_userkind').text();
    //var username = $('ctl00_user').text();
    if (userkind == '超级管理员')
    {
        $('#nvrUserDL').css("display", "block");
    }
    else
    {
        $('#nvrUserDL').css("display", "none");
    }

    $('#snIpt').focus(function (e)
    {
        $('#nvrTip').css("display", "none");
        $('#nvrAdd').css("display", "block");
    });

    $('#snIpt').blur(function (e)
    {
        var sn = $('#snIpt').val();
        if (sn != "")
        {
            $.get("add.ashx", { sn: sn }, function (data)
            {
                if (data == "1")  //sn已存在了且被用户添加
                {
                    $('#STRONG2').text("NVR序列号已被用户添加,请与管理员确认");
                    $('#nvrTip').css("display", "block");
                    $('#nvrAdd').css("display", "none");
                }
                else if (data == "2") //设备没有进入到系统中。
                {
                    $('#STRONG2').text("您所要添加的设备不存在，请确认是否在设备列表中存在");
                    $('#nvrTip').css("display", "block");
                    $('#nvrAdd').css("display", "none");
                }
            });
        }
    });

    $('input[id$=nvrCfmPwdIpt]').focus(function (e)
    {
        $('#mainCfmPwdTips').css("display", "block");
        $('#cfmpwdDiv').css("display", "none");
        $('#nvrAdd').css("display", "block");
    });

    $('input[id$=nvrCfmPwdIpt]').blur(function (e)
    {
        var pwd = $('input[id$=nvrPwdIpt]').val();
        var cfmPwd = $('input[id$=nvrCfmPwdIpt]').val();
        if (pwd != cfmPwd)
        {
            $('#mainCfmPwdTips').css("display", "none");
            $('#cfmpwdDiv').css("display", "block");
            $('#nvrAdd').css("display", "none");
        }
    });

    $('input[id$=nvrUserIpt]').focus(function (e)
    {
        $('#usernameTip').css("display", "none");
        $('#nvrAdd').css("display", "block");
    });

    $('input[id$=nvrUserIpt]').blur(function (e)
    {
        var usernameIpt = $('input[id$=nvrUserIpt]').val();
        if (usernameIpt != '') 
        {
            if ($('#ctl00_user').text() == usernameIpt)//不能添加设备到超级管理员名下
            {
                $('#STRONG3').text("不能将设备添加到超级管理员名下，请填写其他普通用户");
                $('#usernameTip').css("display", "block");
                $('#nvrAdd').css("display", "none");
            }
            else
            {
                $.get("add.ashx", { username: usernameIpt }, function (data)
                {
                    if (data == "用户不存在")
                    {
						$('#STRONG3').text("用户不存在,请先注册用户再添加设备");
                        $('#usernameTip').css("display", "block");
                        $('#nvrAdd').css("display", "none");
                    }
                });
            }
        }
    });

    $('#nvrAdd').click(function (e)
    {
        var nvrSn = $('input[id$=snIpt]').val();
        var snAdmin = $('input[id$=nvrAdminIpt]').val();
        var snPwd = $('input[id$=nvrPwdIpt]').val();
        var snLoc = $('input[id$=nvrLocIpt]').val();
        var btnKind = $('#nvrAdd').text();
        var snUsername = "";
        if (userkind == "超级管理员")
        {
            snUsername = $('input[id$=nvrUserIpt]').val();
        }
        else
        {
            snUsername = $('#ctl00_user').text();
        }
        if (nvrSn != "" && snAdmin != "")
        {
            $.get('add.ashx', { nvrSn: nvrSn, snAdmin: snAdmin, snPwd: snPwd,
                snLoc: snLoc, snUsername: snUsername, kind: btnKind
            }, function (data)
            {
                if (data = "添加设备成功")
                {
                    alert(data);
                    location.href = "main.aspx";
                }
            });
        }
        else
        {
            alert("请检查您NVR管理员是否为空");
        }
    });

});